package com.hwpt.hwaccount.feature.feedback;

import static android.content.ContentValues.TAG;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import java.util.List;

public class FeedbackPresenter implements FeedbackContract.Presenter {
    
    private FeedbackContract.View view;
    private FeedbackContract.Model model;
    private List<FeedbackModel.FeedbackType> feedbackTypes;
    
    public FeedbackPresenter(FeedbackContract.Model model) {
        this.model = model;
        this.feedbackTypes = model.loadFeedbackTypes();
    }
    
    @Override
    public void attachView(FeedbackContract.View view) {
        this.view = view;
    }
    
    @Override
    public void detachView() {
        this.view = null;
    }
    
    @Override
    public void onFeedbackTypeClicked() {
        if (view != null) {
            view.showFeedbackTypeDialog();
        }
    }
    
    @Override
    public void onAddImageClicked() {
        // 选择图片的逻辑在Activity中处理，Presenter只负责通知
    }
    
    @Override
    public void onImageSelected(int requestCode, int resultCode, Intent data) {
        if (view != null) {
            view.onImageSelectionResult(requestCode, resultCode, data);
            view.updateImagePreview();
        }
    }

    @Override
    public void onSubmitClicked() {
        if (view == null) return;

        String type = view.getFeedbackType();
        String description = view.getDescription();
        String phone = view.getPhone();
        List<Uri> images = view.getSelectedImages();

        if (!model.validateFeedback(type, description)) {
            if ("请选择问题类型".equals(type)) {
                view.showToast("请选择问题类型");
                return;
            }

            if (description.isEmpty()) {
                view.showToast("请描述问题");
                return;
            }
        }

        model.submitFeedback(type, description, phone, images, new FeedbackContract.Model.OnSubmitCallback() {
            @Override
            public void onSuccess(List<String> imageUrls) {
                // 在主线程中执行UI操作
                if (view != null) {
                    new Handler(Looper.getMainLooper()).post(() -> {
                        if (imageUrls != null && !imageUrls.isEmpty()) {
                            Log.d(TAG, "图片上传成功，URL列表: " + imageUrls);
                        }
                        view.showToast("反馈提交成功");
                        view.clearForm();
                    });
                }
            }

            @Override
            public void onSuccess() {
                // 在主线程中执行UI操作
                if (view != null) {
                    new Handler(Looper.getMainLooper()).post(() -> {
                        view.showToast("反馈提交成功");
                        view.clearForm();
                    });
                }
            }

            @Override
            public void onError(String message) {
                // 在主线程中执行UI操作
                if (view != null) {
                    new Handler(Looper.getMainLooper()).post(() -> {
                        view.showToast(message);
                    });
                }
            }
        });
    }
    
    @Override
    public void onDescriptionChanged(CharSequence s, int start, int before, int count) {
        // 字符计数逻辑可以在View中处理，或者通过Presenter更新View
    }
    
    @Override
    public List<FeedbackModel.FeedbackType> getFeedbackTypes() {
        return feedbackTypes;
    }
}